package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.baseUtil.JWTUtils;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.MessagePO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.entity.StudentPO;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.mapper.MessageMapper;
import com.ajmd.onlineexam.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ajmd
 * @since 2023/3/8 11:35
 */
@Api("学生端接口")
@RestController
@RequestMapping("/student")
//@CrossOrigin
public class StudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private MessageMapper messageMapper;

    /**
     * controller中的方法返回值所用的实体类会被swagger扫描
     * @return
     */

    @ApiOperation("通过account查询学生")
    @GetMapping("/select")
    public CommonResult findStudentByAccount(@RequestParam String account){
        StudentPO student = studentService.findStudentById(account);
        return CommonResult.ok().data("user",student);
    }

    @ApiOperation("查询所有学生")
    @PostMapping("/findPage")
    public CommonResult findAllStudent(@MultiRequestBody Integer userType,
                                        @MultiRequestBody Integer currentPage,
                                       @MultiRequestBody Integer pageSize,
                                       @MultiRequestBody SearchParam searchParam){
        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        Page<StudentPO> pageData = studentService.findAllStudent(currentPage,pageSize,searchParam);
        return CommonResult.ok().data("pageData",pageData);
    }

    @ApiOperation("学生登录")
    @PostMapping("/login")
    public CommonResult studentLogin(@RequestParam String account,
                                     @RequestParam String password){
        //登录数据校验
        if(StringUtils.isBlank(account)){
            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_NOT_EMPTY);
        }
        if(StringUtils.isBlank(password)){
            return CommonResult.error(AppExceptionCodeEnum.PASSWORD_NOT_EMPTY);
        }
        StudentPO studentPO = studentService.findStudentById(account);
        if(ObjectUtils.isEmpty(studentPO)){
            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_NOT_EXISTS);
        }
        if(!studentPO.getPassword().equals(password)){
//        if(!studentPO.getPassword().equals(DigestUtils.md5Hex(password))){
            return CommonResult.error(AppExceptionCodeEnum.PASSWORD_ERROR);
        }
        Map<String, String> map = new HashMap<>();
        map.put("account",account);
        String token = JWTUtils.getToken(map);
        return CommonResult.ok("登录成功").data("token",token).data("user",studentPO);
    }

    @ApiOperation("学生注册")
    @PostMapping("/register")
    public CommonResult registerStudent(@RequestBody StudentPO student){
        //数据校验
        if(StringUtils.isBlank(student.getAccount())){
            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_NOT_EMPTY);
        }
        if(StringUtils.isBlank(student.getPassword())){
            return CommonResult.error(AppExceptionCodeEnum.PASSWORD_NOT_EMPTY);
        }
        StudentPO studentPO = studentService.findStudentById(student.getAccount());
        if(ObjectUtils.isNotEmpty(studentPO)){
            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_EXISTS);
        }
        studentService.studentRegister(student);
        return CommonResult.ok("注册成功");
    }

    @ApiOperation("修改学生信息")
    @PutMapping("/update")
    public CommonResult updateStudentByPO(@RequestBody StudentPO student){
        if(ObjectUtils.isEmpty(student.getTeamId())){
            return CommonResult.error().message("专业信息不能为空！");
        }
        int num = studentService.studentUpdate(student);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_ERR)
                    .appendMessage("未更新任何信息");
        }
        return CommonResult.ok("信息更新成功");
    }
    @ApiOperation("修改学生正面照")
    @PutMapping("/updateImg")
    public CommonResult updateStudentByPO(@RequestParam String userImg,@RequestParam String account){
        if(ObjectUtils.isEmpty(userImg)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        int num = studentService.updateImg(userImg,account);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_ERR)
                    .appendMessage("正面照修改失败");
        }
        return CommonResult.ok("正面照更新成功");
    }

    @ApiOperation("重置学生密码")
    @PutMapping("/resetPassword")
    public CommonResult updateStudentPassword(@RequestParam Integer userType,@RequestParam String account){
        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        int num = studentService.resetPassword(account);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_ERR)
                    .appendMessage("重置密码失败");
        }
        return CommonResult.ok("重置密码成功");
    }

    @ApiOperation("删除学生信息")
    @DeleteMapping("/delete")
    public CommonResult deleteStudentById(@RequestParam Integer userType,@RequestParam String account){
        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        int num = studentService.delete(account);
        if(num==0){
            return CommonResult.error().message("删除失败");
        }
        return CommonResult.ok().message("删除成功");
    }


    @GetMapping("/getMessages")
    public CommonResult getMessages(@RequestParam String account){
        LambdaQueryWrapper<MessagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MessagePO::getReceiver,account)
                .orderByDesc(MessagePO::getSendTime);
        List<MessagePO> messagePOList = messageMapper.selectList(queryWrapper);
        return CommonResult.ok().data("messagePOList",messagePOList);
    }

    @GetMapping("/test")
    public CommonResult test(){
        System.out.println("进入方法2");
        StudentPO student = studentService.findStudentById("2009034843001");
        System.out.println(student);
        return CommonResult.ok().data("student",student);
    }

}
